import {MissonModel} from '@/mount/room/Misson/missonModel'
import { colorful, isInArray } from '@/utils'
/* 采矿相关任务集合 */
export default {
    /* 原矿任务 */
    Mine:{
        harvest(myRoom:string):string{
            var thisRoom = Game.rooms[myRoom]
            if (!thisRoom) return `[Mine] 不存在该房间!`
            var thisTask:RoomTask = {} as RoomTask
            Object.assign(thisTask,MissonModel['原矿开采'])
            var mineral_ = Game.getObjectById(thisRoom.memory.TaskMemory.mineralID as string) as Mineral
            if (!mineral_) return `[Mine] 未找到房间${myRoom}的矿源`
            var rType = mineral_.mineralType
            thisTask.relateResource = [rType]
            thisTask.relateCreep = ['miner']
            if(thisRoom.mountMisson(thisTask))
            return `[Mine] 房间${myRoom}挂载原矿开采任务成功!`
            else return `[Mine] 原矿开采任务挂载失败!`
        },
        Charvest(myRoom:string):string{
            var thisRoom = Game.rooms[myRoom]
            if (!thisRoom) return `[Mine] 不存在该房间!`
            if (thisRoom.deleteMisson('CREEP','原矿开采'))
            return  `[Mine] 房间${myRoom}原矿开采任务删除成功!`
            else return `[Mine] 房间${myRoom}不存在原矿开采任务!`
        },
        /* 设置仓库的极限矿物容纳量，超过该容量如果有采矿任务将自动删除 */
        setnum(myRoom:string,num:number):string{
            var thisRoom = Game.rooms[myRoom]
            if (!thisRoom) return `[Mine] 不存在该房间!`
            thisRoom.memory.TaskData.mineralNum =num
            return `[Mine] 房间${myRoom}原矿开采极限矿物数量已经设置为${num}`
            
        }
    },
    
    /* 外矿任务 */
    OutMine:{
        harvest(myRoom:string,disRoom:string,num:number):string{
            var thisRoom = Game.rooms[myRoom]
            if (!thisRoom) return `[OutMine] 不存在该房间!`
            var thisTask:RoomTask = {} as RoomTask
            Object.assign(thisTask,MissonModel['外矿开采'])
            thisTask.disRoom = [disRoom]
            thisTask.num = num
            thisTask.relateCreep = ['out-claim','out-defend','out-min','out-car']
            thisTask.numData = {'out-claim':1,'out-defend':0,'out-min':num,'out-car':num}
            thisTask.bindData = {'out-claim':{num:1,bind:[]},
                                'out-defend':{num:0,bind:[]},
                                'out-min':{num:num,bind:[]},
                                'out-car':{num:num,bind:[]}
                                }
            if(thisRoom.mountMisson(thisTask))
            return `[OutMine]挂载外矿任务 ${myRoom}-->${disRoom} 成功!`
            else return `[OutMine]挂载外矿任务 ${myRoom}-->${disRoom} 失败!`
        },
        Charvest(myRoom:string,disRoom:string):string{
            var thisRoom = Game.rooms[myRoom]
            if (!thisRoom) return `[OutMine] 不存在该房间!`
            for (var i of thisRoom.memory.Task['CREEP'])
            {
                if (i.Name == '外矿开采' && i.disRoom[0] == disRoom)
                if (thisRoom.deleteMisson_ID(i.ID))
                    return `[OutMine]删除外矿任务 ${myRoom}-->${disRoom} 成功!`
            }
            return `[OutMine]删除外矿任务 ${myRoom}-->${disRoom} 失败!`
        },
        setnum(myRoom:string,disRoom:string,num:number):string{
            var thisRoom = Game.rooms[myRoom]
            if (!thisRoom) return `[OutMine] 不存在该房间!`
            for (var i of thisRoom.memory.Task['CREEP'])
            {
                if (i.Name == '外矿开采' && i.disRoom[0] == disRoom)
                {
                    i.num = num
                    return `[OutMine]外矿任务 ${myRoom}-->${disRoom}num修改成功!`
                }
            }
            return `[OutMine]未找到 ${myRoom}-->${disRoom}的任务!`
        },
        road(myRoom:string,disRoom:string):string{
            var thisRoom = Game.rooms[myRoom]
            if (!thisRoom) return `[OutMine] 不存在该房间!`
            var startFlagName = `${myRoom}/${disRoom}/S`
            var endFlagName = `${myRoom}/${disRoom}/E`
            if (!Flag[startFlagName] || !Flag[endFlagName]) return `[OutMine] 未找到对应旗帜`
            
        }
    },

    /* 路径计算器 */
    path:{
        calculate(x,y,roomName,x_,y_,disRoom,maxOps:number = 12000):string{
            var a = Game.cpu.getUsed()
            var result = new RoomPosition(x,y,roomName).GetPath(new RoomPosition(x_,y_,disRoom),1,maxOps)
            var b = Game.cpu.getUsed()
            if (result == null) return colorful(`[path]房间${roomName}[${x},${y}] --> 房间${disRoom}[${x_},${y_}]寻路ops点超过${maxOps}，寻路失败！`,'red',true)
            var reStr = `[path]房间${roomName}[${x},${y}] --> 房间${disRoom}[${x_},${y_}]的寻路结果如下:\n`
            reStr += `    cost:${result.cost} cpu:${b-a} 路程:${result.RoomPostions.length}  [已经加入全局缓存]`
            return colorful(reStr,'green',true)
        }
    },

    /* PowerBank任务 */
    PowerBank:{
        open(roomName:string):string{
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[PowerBank]未找到房间${roomName},请确认房间!`
            if (myRoom.controller.level < 8) return `[PowerBank]房间控制等级不足!`
            if (!myRoom.memory.TaskMemory.ObserverID) return `[PowerBank]未找到观察者建筑!`
            var thisTask:RoomTask = {} as RoomTask
            Object.assign(thisTask,MissonModel['power采集'])
            thisTask.disRoom = []
            if (myRoom.mountMisson(thisTask)) return colorful(`[PowerBank]房间${roomName}的power采集任务挂载成功!`,'green',true)
            else  colorful(`[PowerBank]房间${roomName}的power采集任务挂载失败!`,'red',true)
            },
        close(roomName:string):string{
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[PowerBank]未找到房间${roomName},请确认房间!`
            if (myRoom.deleteMisson('ROOM','power采集'))
            return  `[PowerBank]房间${roomName}power采集任务删除成功!`
            else
            return `[PowerBank]房间${roomName}未找到power采集任务或删除任务失败!`
        },
        add(roomName:string,disRooms:string[]):string{
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[PowerBank]未找到房间${roomName},请确认房间!`
            var thisTask = myRoom.getMisson("ROOM",'power采集')
            if (!thisTask) return `[PowerBank]未找到采集任务!`
            for (var i of disRooms)
            {
                if (!isInArray(thisTask.disRoom,i)) thisTask.disRoom.push(i)
            }
            return `[PowerBank]房间${roomName}已经添加目标房间!`
        },
        clean(roomName:string):string{
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[PowerBank]未找到房间${roomName},请确认房间!`
            var thisTask = myRoom.getMisson("ROOM",'power采集')
            if (!thisTask) return `[PowerBank]未找到采集任务!`
            thisTask.disRoom = []
            return `[PowerBank]房间${roomName}的PowerBank任务已经清除目标房间列表!`
        },
        remove(roomName:string,disRoom:string){
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[PowerBank]未找到房间${roomName},请确认房间!`
            var thisTask = myRoom.getMisson("ROOM",'power采集')
            if (!thisTask) return `[PowerBank]未找到采集任务!`
            _.without(thisTask.disRoom,disRoom)
            return `[PowerBank]房间${roomName}的PowerBank任务已经清除目标房间${disRoom}!`
        },
        show(roomName:string):string{
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[PowerBank]未找到房间${roomName},请确认房间!`
            var thisTask = myRoom.getMisson("ROOM",'power采集')
            if (!thisTask) return `[PowerBank]未找到采集任务!`
            var result:string = ''
            for (var i of thisTask.disRoom) result += i + ' '
            return result
        }
    },

    /* 烧power任务 */
    Power:{
        open(roomName:string):string{
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[Power]未找到房间${roomName},请确认房间!`
            if (myRoom.controller.level < 8) return `[Power]房间不足8级!`
            var powerSpawn = Game.getObjectById(myRoom.memory.TaskMemory.PowerSpawnID as string) as StructurePowerSpawn
            if (!powerSpawn) return `[Power]房间${roomName}未找到PowerSpawn!`
            /* 都满足条件后，开启烧power任务 */
            var thisTask:RoomTask = {} as RoomTask
            Object.assign(thisTask,MissonModel['power升级'])
            thisTask.relateStructure = [powerSpawn.id]
            if (myRoom.mountMisson(thisTask))
            {
                return `[Power]房间${roomName}的Power冲级任务挂载成功!`
            }
            else
                return colorful(`[Power]房间${roomName}的Power冲级任务挂载失败!`,'red')
        },
        close(roomName:string):string{
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[Power]未找到房间${roomName},请确认房间!`
            var result = myRoom.deleteMisson('STRUCTURE','power升级')
            return `[Power]房间${roomName}的Power冲级任务删除返回码为${result}!`
        }
    },

    /* 搓像素任务 */
    Pixel:{
        open(roomName:string,disRoom:string):string{
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[Power]未找到房间${roomName},请确认房间!`
            var thisTask:RoomTask = {} as RoomTask
            Object.assign(thisTask,MissonModel['搓pixel'])
            thisTask.disRoom = [disRoom]
            if (myRoom.mountMisson(thisTask)) return `[Pixel]房间${roomName}搓像素任务挂载成功!`
            return colorful(`[Pixel]房间${roomName}搓像素任务挂载失败!`,'red')
        },
        close(roomName:string):string{
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[Power]未找到房间${roomName},请确认房间!`
            var result = myRoom.deleteMisson('ROOM','搓pixel')
            return `[Pixel]房间${roomName}的搓pixel任务删除返回码为${result}!`
        }
    }
}